#!/bin/bash

# biz分支
#echo 切换biz分支
#git checkout biz
#git pull

# master分支
#echo 切换master分支
#git checkout master
git pull

targetPath="/opt"
time=$(date "+%Y%m%d%H%M%S")

echo 清理并重新打包
mvn clean package -Dmaven.test.skip=true


    mv ${targetPath}/red_packet.jar ${targetPath}/bak/red_packet.jar${time}
    mv ${targetPath}/account.jar ${targetPath}/bak/account.jar${time}
    mv ${targetPath}/user_center_v2.jar ${targetPath}/bak/user_center_v2.jar${time}
    mv ${targetPath}/task.jar ${targetPath}/bak/task.jar${time}
    mv ${targetPath}/sm_home.jar ${targetPath}/bak/sm_home.jar${time}
    mv ${targetPath}/biz-controller.jar ${targetPath}/bak/biz-controller.jar${time}

    # 备份项目
    cp ./red-packet/red-packet-controller/target/red_packet.jar ${targetPath}
    cp ./account/account_controller/target/account.jar ${targetPath}
    cp ./ucenter/user-controller/target/user_center_v2.jar ${targetPath}
    cp ./timed-task/timed-task-service/target/task.jar ${targetPath}
    cp ./sm-home/sm-home-controller/target/sm_home.jar ${targetPath}
    cp ./biz/biz-controller/target/biz-controller.jar ${targetPath}


    # 停止项目 START
    ############
    pid=`ps -ef | grep red_packet.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi
    # 停止 red_packet

    pid=`ps -ef | grep account.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi
    # 停止 account


    pid=`ps -ef | grep user_center_v2.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi
    # 停止 user_center_v2

    pid=`ps -ef | grep task.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi
    # 停止task

    pid=`ps -ef | grep sm_home.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi

    # 停止 sm_home

    pid=`ps -ef | grep biz-controller.jar | grep -v grep | awk '{print $2}'`
    if [ -n "$pid" ]
    then
    #!kill -9 强制终止
       echo "kill -9 的pid:" $pid
       kill -9 $pid
    fi

    # 停止 biz-controller
    # 停止项目  END

    # 启动项目
    sh ${targetPath}/start.sh ${targetPath}/red_packet.jar 8083
    sh ${targetPath}/start.sh ${targetPath}/account.jar 8082
    sh ${targetPath}/start.sh ${targetPath}/user_center_v2.jar 8081
    sh ${targetPath}/start.sh ${targetPath}/task.jar 8090
    sh ${targetPath}/start.sh ${targetPath}/sm_home.jar 8084
    sh ${targetPath}/start.sh ${targetPath}/biz-controller.jar 8087


    echo user_center_v2.jar 8081
    echo account.jar 8082
    echo red_packet.jar 8083
    echo task.jar 8090
    echo sm_home.jar 8084
    echo biz-controller.jar 8087
    echo 已重新打包部署完成

fi